<?php
class Show extends Info
{
	public function __construct()
    {
        parent::__construct();
    }	
    
    public function Index()
    {
        parent::Index();
    }
    
    public function Add()
    {
        $this->mod="Add";
        $categoryid=intval($_GET['categoryid']);
        $category=DB::fetch_row("select * from bb_category as C left join bb_category_info as CI on C.categoryid=CI.categoryid where C.categoryid='{$categoryid}'");
        $this->_T['category']=$category;
        date_Default_TimeZone_set("PRC");
        $this->_T['newstime']=date("Y-m-d H:i:s");
        //$array=array(array(),array(),array(),array(),array(),array());
        $this->_T['list']=$array;
        $this->template("parent");
    }
    
    public function Create()
    {
       $property=implode("|", $_POST['property']);
       $objectid=File::uploadFile("titlepic","image","storage");
       $titlepic=DB::fetch_row("select realname from bb_file where objectid='{$objectid}'");
       $categoryid=$_POST['categoryid'];
       //bb_info表
       $info=array();
       $info['categoryid']=$categoryid;
       $info['mid']=2;
       $info['userid']=$_SESSION['U']['userid'];
       $info['createtime']=time();
       $info['newstime']=strtotime($_POST['newstime']);
       $info['titlepic']=$titlepic['realname'];
       $info['headline']=strstr($property, "headline")?1:0;
       $info['commend']=strstr($property, "commend")?1:0;
       DB::begin();
       $infoid=DB::insert("bb_info", $info);
       if(!$infoid)
       {
           DB::rollback();
           $this->showmessage("信息添加失败，错误代码1","goback" , 2000,"返回");
       }
       //bb_info_info表
       $info_info=array();
       $info_info["infoid"]=$infoid;
       $info_info["title"]=$_POST['title'];
       $info_info["ftitle"]=$_POST['ftitle'];
       $info_info["keywords"]=$_POST['keywords'];
       $info_info["smalltext"]=$_POST['smalltext'];
       $info_info["writer"]=$_POST['writer'];
       $info_info["befrom"]=$_POST['befrom'];
       if(!DB::insert("bb_info_info", $info_info))
       {
           DB::rollback();
           $this->showmessage("信息添加失败，错误代码2", "goback", 2000,"返回");
       }
       //bb_info_news表
       $info_news=array();
       $info_news["infoid"]=$infoid;
       $info_news["newstext"]=$_POST["newstext"];
       if(!DB::insert("bb_info_news", $info_news))
       {
           DB::rollback();
           $this->showmessage("信息添加失败，错误代号3","goback" ,2000, "返回");
       }
       //bb_show_label 
       $show_label=array();
       $labelkey=$_POST['labelkey'];
       $labelvalue=$_POST['labelvalue'];
       $l_order=$_POST['l_order'];
       $num=count($labelkey);
       for($i=0;$i<$num;$i++)
       {
           if($labelkey[$i]!=null)//以标签键是否存在为标准
           {
               $show_label["lkey"]=$labelkey[$i];
               $show_label["lvalue"]=$labelvalue[$i];
               $show_label["lorder"]=intval($l_order[$i]);
               $show_label["infoid"]=intval($infoid);
               if(!DB::insert("bb_show_label", $show_label))
               {
                   DB::rollback();
                   $this->showmessage("标签添加失败，错误代号{$i}","goback" ,2000, "返回");
               }
                  
           }
         
       }
       //bb_show_img
       $show_img=array();
       for($i=0;$i<count($_POST['order']);$i++)
       {
           $name="files_".$i;
           if($_FILES[$name]['size']>0)
           {
               $objectid=File::uploadFile($name, "image", "storage");
               $show_img["alt"]=$_POST['alt'][$i];
               $show_img["launch"]=($_POST['launch'][$i]=="on")?1:0;
               $show_img["picintro"]=$_POST['picintro'][$i];
               $show_img["pattern"]=$_POST['pattern'][$i];
               $show_img["i_order"]=$_POST['order'][$i];
               $show_img['infoid']=$infoid;
               $show_img['objectid']=$objectid;
               if(!DB::insert("bb_show_img", $show_img))
               {
                   DB::rollback();
                   $this->showmessage("信息增加失败，错误代号0","goback" ,2000, "返回");                  
               }
           }
           elseif (isset($_POST['picintro'][$i])) {
               $show_img["alt"]=$_POST['alt'][$i];
               $show_img["launch"]=($_POST['launch'][$i]=="on")?1:0;
               $show_img["picintro"]=$_POST['picintro'][$i];
               $show_img["pattern"]=$_POST['pattern'][$i];
               $show_img["i_order"]=$_POST['order'][$i];
               $show_img['infoid']=$infoid;
               $show_img['objectid']=null;
               if(!DB::insert("bb_show_img", $show_img))
               {
                   DB::rollback();
                   $this->showmessage("信息增加失败，错误代号1","goback" ,2000, "返回");                  
               }
           }
       }
       DB::commit();
       $this->showmessage("信息添加成功", "?m=Admin/Info/Show/Index&categoryid={$categoryid}", 2000,"返回");
    }
    
    public function Edit()
    {
        $this->mod="Edit";
        $infoid=$_GET['infoid'];
        $categoryid=$_GET['categoryid'];
        $info=DB::fetch_row("select * from bb_info as A left join bb_info_info as B on A.infoid = B.infoid where A.infoid = '{$infoid}'");
        $info_news=DB::fetch_row("select * from bb_info_news where infoid='{$infoid}'");
        $show_label=DB::fetch_array("select * from bb_show_label where infoid='{$infoid}'");
        $show_img=DB::fetch_array("select * from bb_show_img where infoid='{$infoid}'");
        for ($i=0; $i < count($show_img); $i++)
        { 
            if($show_img[$i]["objectid"]!=0)
            {
                $objectid=$show_img[$i]["objectid"];
                $imgs=DB::fetch_row("select realname from bb_file where objectid='".$objectid."'");
                $show_img[$i]["realname"]=$imgs[0];
            }
        }
        $this->_T['info']=$info;
        $this->_T['info_news']=$info_news;
        $this->_T['show_label']=$show_label;
        $this->_T['show_img']=$show_img;
        $category=DB::fetch_row("select *from `bb_category` as C left join `bb_category_info` as CI on C.`categoryid` = CI.`categoryid` where C.`categoryid`='{$categoryid}'");
        $this->_T['category']=$category;
        $this->_T['newstime']=date("Y-m-d H:i:s",$info["newstime"]);                       
        $this->template("parent");
    }
    
    public function update()
    {
        $categoryid=$_POST['categoryid'];
        $property = implode("|",$_POST['property']);
        $objectid = File::uploadFile("titlepic", "image", "storage");
        $imgageFile = DB::fetch_row("select *from `bb_file` where `objectid`='{$objectid}'");
        $newstime = strtotime($_POST['newstime']);
        $infoid   = $_POST['infoid'];
        $titlepic = "";
        if($_POST['oldtitlepic']&&$imgageFile){
            $info = DB::fetch_row("select *from `bb_info` where `infoid`='{$infoid}'");
            File::deleteFile($info['titlepic'], "delete file");
            $titlepic = $imgageFile['realname'];
        }else if($_POST['oldtitlepic']){
            $titlepic = $_POST['oldtitlepic'];
        }else if($imgageFile){
            $titlepic = $imgageFile['realname'];
        }
        $condition = array();
        $condition['infoid'] = $infoid;
        DB::begin();
        $info = array();
        $info['newstime']      = $newstime;
        $info['titlepic']      = $titlepic;
        $info['headline']      = strstr($property,"headline")?1:0;
        $info['commend']       = strstr($property,"commend")?1:0;
        if(!DB::update("bb_info", $info, $condition)){
            DB::rollback();
            $this->showmessage("信息更新失败，错误代号1","goback" ,2000, "返回");
        }
        $info_info['title']    = $_POST['title'];
        $info_info['ftitle']   = $_POST['ftitle'];
        $info_info['keywords'] = $_POST['keywords'];
        $info_info['smalltext']= $_POST['smalltext'];
        $info_info['writer']   = $_POST['writer'];
        $info_info['befrom']   = $_POST['befrom'];
        if(!DB::update("bb_info_info", $info_info, $condition)){
            DB::rollback();
            $this->showmessage("信息更新失败，错误代号2","goback" ,2000, "返回");
        }
        $info_news['newstext'] = $_POST['newstext'];
        if(!DB::update("bb_info_news", $info_news, $condition)){
            DB::rollback();
            $this->showmessage("信息更新失败，错误代号3","goback" ,2000, "返回");
        }
        DB::commit();
        $this->showmessage("信息更新成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
    }
    
    public function Delete()
    {
        
    }
}
?>